package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.ServiceDto;
import com.csun.cmny.provider.dto.ServiceQueryDto;
import com.csun.cmny.provider.model.domain.OmcService;
import com.csun.cmny.provider.model.domain.OmcServiceFirstLevel;
import com.csun.cmny.provider.model.domain.OmcServiceSecondLevel;
import com.csun.cmny.provider.model.domain.OmcServiceThirdLevel;
import com.csun.cmny.provider.model.dto.ServiceFirstLevelDto;
import com.csun.cmny.provider.model.dto.ServiceSecondLevelDto;
import com.csun.cmny.provider.model.dto.ServiceThirdLevelDto;
import com.csun.cmny.provider.model.dto.ServiceThirdLevelQueryDto;
import com.csun.cmny.provider.model.vo.ServiceFirstLevelVO;
import com.csun.cmny.provider.model.vo.ServiceThirdLevelVO;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcServiceFirstLevelService;
import com.csun.cmny.provider.service.OmcServiceSecondLevelService;
import com.csun.cmny.provider.service.OmcServiceService;
import com.csun.cmny.provider.service.OmcServiceThirdLevelService;
import com.csun.cmny.provider.validator.OmcServiceAddValidator;
import com.csun.cmny.provider.validator.OmcServiceEditValidator;
import com.csun.cmny.provider.validator.ServiceFirstLevelDtoValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: xcr
 * @Date: 2019-05-25
 * @Description: the class omc Service controller
 */
@RestController
@RequestMapping(value = "/service", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcServiceController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcServiceService omcServiceService;
    @Resource
    private OmcServiceFirstLevelService omcServiceFirstLevelService;
    @Resource
    private OmcServiceSecondLevelService omcServiceSecondLevelService;
    @Resource
    private OmcServiceThirdLevelService omcServiceThirdLevelService;

    /*
     * 添加服务
     * */
    @ValidateAnnotation
    @PostMapping(value = "/add")
    public Wrapper addService(@Validated(OmcServiceAddValidator.class) @RequestBody ServiceDto serviceDto,
                              BindingResult br) {
        serviceDto.setGroupId(getLoginAuthDto().getGroupId());
        Integer result = saveService(serviceDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //返回服务id
        OmcService omcService = new OmcService();
        BeanUtils.copyProperties(serviceDto, omcService);
        omcService = omcServiceService.selectOne(omcService);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, omcService);
    }

    /*
     * 服务详情
     * */
    @PostMapping(value = "/getById/{serviceId}")
    public Wrapper getServiceById(@PathVariable Long serviceId) {

        // 校验Id是否为空
        Preconditions.checkArgument(serviceId != null, cmnyMessageSource
                .getMessage("service.id.null", null));

        OmcService omcService = omcServiceService.selectByKey(serviceId);
        if (omcService == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.null", null));
        }
        //return WrapMapper.ok(omcService);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), omcService);
    }

    /*
     * 服务修改
     * */
    @ValidateAnnotation
    @PostMapping(value = "/edit")
    public Wrapper editService(@Validated(OmcServiceEditValidator.class) @RequestBody ServiceDto serviceDto,
                               BindingResult br) {

        //校验服务次数、订单次数是否为0
        OmcService omcService = omcServiceService.selectByKey(serviceDto.getId());
        if (omcService == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.null", null));
        }
        if (omcService.getOrderTimes() > 0 || omcService.getServiceTimes() > 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.times.edit", null));
        }
        //修改
        Integer result = saveService(serviceDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /*
     * 分页查询
     * */
    // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryServiceListWithPage(@RequestBody ServiceQueryDto serviceQueryDto) {
        serviceQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = omcServiceService.queryServiceListWithPage(serviceQueryDto);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /*
     * 删除服务
     * */
    @PostMapping(value = "/deleteById/{serviceId}")
    public Wrapper deleteService(@PathVariable Long serviceId) {

        // 校验Id是否为空
        Preconditions.checkArgument(serviceId != null, cmnyMessageSource
                .getMessage("service.id.null", null));
        OmcService omcService = omcServiceService.selectByKey(serviceId);
        //校验服务次数、订单次数是否为0
        if (omcService == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.null", null));
        }
        if (omcService.getOrderTimes() > 0 || omcService.getServiceTimes() > 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.times.delete", null));
        }


        //校验是否绑定有二级服务类别
        OmcServiceSecondLevel omcServiceSecondLevel = new OmcServiceSecondLevel();
        omcServiceSecondLevel.setFirstLevelId(serviceId);
        List<OmcServiceSecondLevel> select = omcServiceSecondLevelService.select(omcServiceSecondLevel);
        if (PublicUtil.isNotEmpty(select)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("second.is.not.null.error", null));
        }


        Integer result = omcServiceService.deleteServiceAndItem(serviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /*
     * 服务保存
     * */
    public Integer saveService(ServiceDto serviceDto) {
        OmcService omcService = new OmcService();
        BeanUtils.copyProperties(serviceDto, omcService);
        return omcServiceService.saveService(omcService, getLoginAuthDto());
    }

    /*
     * 三助一护-添加一级服务菜单
     * */
    @PostMapping(value = "/addFirstLevel")
    public Wrapper addServiceFirstLevel(@RequestBody ServiceFirstLevelDto serviceFirstLevelDto,
                                        BindingResult br) {
        serviceFirstLevelDto.setGroupId(getLoginAuthDto().getGroupId());
        Integer i = saveServiceFirstLevel(serviceFirstLevelDto, "save");
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-查询一级菜单及以下二级菜单
     * */
    @PostMapping(value = "/listService")
    public Wrapper listService(@RequestBody ServiceQueryDto serviceQueryDto) {
        List<ServiceFirstLevelVO> voList = omcServiceFirstLevelService.listService(serviceQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, voList);
    }

    /*
     * 三助一护-分页查询
     * */
    @PostMapping(value = "/listServicePage")
    public Wrapper listPage(@RequestBody ServiceQueryDto serviceQueryDto) {
        PageInfo pageInfo = omcServiceFirstLevelService.listPage(serviceQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/listPage")
    public Wrapper listPage(@RequestBody ServiceThirdLevelQueryDto serviceThirdLevelQueryDto) {
        PageInfo pageInfo = omcServiceThirdLevelService.listPage(serviceThirdLevelQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }

    /*
     * 三助一护-删除一级服务菜单
     * */
    @PostMapping(value = "/deleteFirstLevel/{firstLevelId}")
    public Wrapper deleteFirstLevel(@PathVariable Long firstLevelId) {
        int i = omcServiceFirstLevelService.deleteFirstLevel(firstLevelId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-删除二级服务菜单
     * */
    @PostMapping(value = "/deleteSecondLevel/{secondLevelId}")
    public Wrapper deleteSecondLevel(@PathVariable Long secondLevelId) {
        int i = omcServiceSecondLevelService.deleteSecondLevel(secondLevelId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-删除三级服务菜单
     * */
    @PostMapping(value = "/deleteThirdLevel/{thirdLevelId}")
    public Wrapper deleteThirdLevel(@PathVariable Long thirdLevelId) {
        int i = omcServiceThirdLevelService.deleteThirdLevel(thirdLevelId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-修改一级服务菜单
     * */
    @PostMapping(value = "/updateFirstLevel")
    public Wrapper updateFirstLevel(@RequestBody ServiceFirstLevelDto serviceFirstLevelDto,
                                    BindingResult br) {
        Integer i = saveServiceFirstLevel(serviceFirstLevelDto, "update");
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-添加二级服务菜单
     * */
    @PostMapping(value = "/addSecondLevel")
    public Wrapper addSecondLevel(@RequestBody ServiceSecondLevelDto serviceSecondLevelDto,
                                  BindingResult br) {
        Integer i = saveServiceSecondLevel(serviceSecondLevelDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-修改二级服务菜单
     * */
    @PostMapping(value = "/updateSecondLevel")
    public Wrapper updateSecondLevel(@RequestBody ServiceSecondLevelDto serviceSecondLevelDto,
                                     BindingResult br) {
        Integer i = saveServiceSecondLevel(serviceSecondLevelDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-添加二级服务菜单
     * */
    @PostMapping(value = "/addThirdLevel")
    public Wrapper addThirdLevel(@RequestBody ServiceThirdLevelDto serviceThirdLevelDto,
                                 BindingResult br) {
        Integer i = saveServiceThirdLevel(serviceThirdLevelDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 三助一护-修改二级服务菜单
     * */
    @PostMapping(value = "/updateThirdLevel")
    public Wrapper updateThirdLevel(@RequestBody ServiceThirdLevelDto serviceThirdLevelDto,
                                    BindingResult br) {
        Integer i = saveServiceThirdLevel(serviceThirdLevelDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, i);
    }

    /*
     * 保存服务二级菜单
     * */
    private Integer saveServiceSecondLevel(ServiceSecondLevelDto serviceSecondLevelDto) {
        OmcServiceSecondLevel omcServiceSecondLevel = new OmcServiceSecondLevel();
        BeanUtils.copyProperties(serviceSecondLevelDto, omcServiceSecondLevel);
        return omcServiceSecondLevelService.saveServiceSecondLevel(omcServiceSecondLevel, getLoginAuthDto());
    }

    /*
     * 保存服务一级菜单
     * */
    public Integer saveServiceFirstLevel(ServiceFirstLevelDto serviceFirstLevelDto, String type) {
        OmcServiceFirstLevel omcServiceFirstLevel = OmcServiceFirstLevel.builder().build();
        BeanUtils.copyProperties(serviceFirstLevelDto, omcServiceFirstLevel);
        return omcServiceFirstLevelService.saveServiceFirstLevel(omcServiceFirstLevel, getLoginAuthDto(), type);
    }

    /*
     * 三助一护-保存三级菜单
     * */
    private Integer saveServiceThirdLevel(ServiceThirdLevelDto serviceThirdLevelDto) {
        OmcServiceThirdLevel omcServiceThirdLevel = new OmcServiceThirdLevel();
        BeanUtils.copyProperties(serviceThirdLevelDto, omcServiceThirdLevel);
        return omcServiceThirdLevelService.saveServiceThirdLevel(omcServiceThirdLevel, getLoginAuthDto());
    }

    @PostMapping(value = "/testOmcService")
    public Wrapper testOmcService(@Validated(OmcServiceAddValidator.class) @RequestBody ServiceDto serviceDto) {

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, null);
    }
}
